DodoRunner.DodoModel = (function($){
    //var STATE_STANDING 	= "STANDING",
  var   STATE_RUNNING 		= "run",
        STATE_JUMPING 		= "jump",
        STATE_SLIDING 		= "slide",
        STATE_DYING 		= "die",
        STATE_DEAD 			= "dead";
      //  STATE_LAYING_EGG 	= "LAYING_EGG";

    var jumpsLeft = 5;

    var MOVEMENTSPEED = 0.3;

    var observable = {};
    var x = 0,
        y = 550,
        state = STATE_RUNNING;

    var init = function() {
        initKeyEvents();
        moveRight(0);
    },

    initKeyEvents = function(){
        DodoRunner.GameController.on("keyPress", function(event, charCode){
            switch(charCode){
                case 120: {
                    if(jumpsLeft > 0){
                        jumpsLeft--;
                        $($(".jump")[0]).remove();
                        jump();
                        setTimeout(function(){
                            jumpsLeft++;
                            $("#jumps").append('<img class="jump" src="res/images/jump.png">');
                        }, 3000);
                    }
                } break;
                case 121: slide(); break;
            }
        });
    },

    run = function() {
        setState(STATE_RUNNING);
    },

    jump = function() {
        setState(STATE_JUMPING);
    },

    slide = function() {
        setState(STATE_SLIDING);
    },

    die = function() {
        setState(STATE_DYING);
    },

    dead = function(){
        setState(STATE_DEAD);
    },

    isJumping = function (){
        return state == STATE_JUMPING;
    },

    isSliding = function(){
        return state == STATE_SLIDING;
    },

    isRunning = function(){
        return state == STATE_RUNNING;
    },

    setState = function(_state){
        state = _state;
        $(observable).trigger("stateChanged", state);
    },

    getX = function(){
        return x;
    },

    getY = function(){
        return y;
    },

    setX = function(_x){
        x = _x;
    },

    setY = function(_y){
        y = _y;
    },

    getWidth = function(){
        return 144;
    },

    getHeight = function(){
        return 144;
    },

    moveRight = function(){
         var dt = createjs.Ticker.getInterval();
         x += MOVEMENTSPEED*dt;
         $(observable).trigger("move",x);
         requestAnimationFrame(moveRight);
    };

    return {
        init : init,
        jump: jump,
        run: run,
        die: die,
        dead: dead,
        isJumping : isJumping,
        isRunning : isRunning,
        isSliding : isSliding,
        slide : slide,
        getX : getX,
        getY : getY,
        setX : setX,
        setY : setY,
        getWidth : getWidth,
        getHeight : getHeight,
        on : function(event, callback){
            return $(observable).on(event, callback);
        }
    }

}(jQuery));

DodoRunner.DodoView = (function($, _, app, easel){

var spriteSheet,
    sprite,

    JUMP_HEIGHT = 5,
    ANIM_LENGTH = 42 + 3,

    animTime = 3,
    animStep = 0,
    dt = 0,

    init = function() {
        initSprite();
        initEvents();
        initAnimiationEvents();
        updateAnimation(0);
    },

    initAnimiationEvents = function(){
        sprite.onAnimationEnd = function(c, endAnimation){
            switch (endAnimation){
                case "jump" : {
                    DodoRunner.DodoModel.slide();
                } break;
            }
        }
    },

    initSprite = function(){
        spriteSheet = new easel.SpriteSheet({
            images: [app.AssetManager.getImageByName("dodo")],
            frames: {
                width: 144,
                height: 144
            },
            animations: {
                run	    :[ 0,  7],
                jump	:[ 8,  14, "slide"],
                slideIn	:[ 16, 22],
                slide   :[ 25, 25],
                slideOut:[ 23, 26],
                die	    :[ 26, 26],
                dead    :[ 26, 26]
            }
        });
        sprite = new createjs.BitmapAnimation(spriteSheet);
        sprite.gotoAndStop("run");
        sprite.x = DodoRunner.DodoModel.getX();
        sprite.y = DodoRunner.DodoModel.getY();
        sprite.z = 2;
        app.Canvas.addChild(sprite);
    },

    initEvents = function(){
        DodoRunner.DodoModel.on("stateChanged", function(event, state){
            animStep = 0;
            sprite.gotoAndStop(state);
        });
        DodoRunner.DodoModel.on("move", function(event, x){
            sprite.x = x;
        });
    },

    updateAnimation = function(){
        dt += 1;
        jumpAnimation();
        animStep += 1;
        if(dt > animTime){
            dt = 0;
            sprite.advance();
        }
        requestAnimationFrame(updateAnimation);
    },

    jumpAnimation = function(){
        if(DodoRunner.DodoModel.isJumping()) {
            sprite.y -= (animStep < ANIM_LENGTH/2 ? 1 : -1) * ((animStep/ANIM_LENGTH)^2) * JUMP_HEIGHT;
            dt-= 0.5;
        }
        else{
            if(sprite.y < 550){
                sprite.y += 7;
            }
            else{
                if(!DodoRunner.DodoModel.isRunning())
                    DodoRunner.DodoModel.run();
                sprite.y = 550;
            }
        }
        DodoRunner.DodoModel.setY(sprite.y);
    }

    return {
        init : init
    }

}(jQuery, _, DodoRunner, createjs));

DodoRunner.DodoSound = (function($, app){
var
    init = function() {
        initSounds();
        initEvents();
    },

    initEvents = function(){
        app.DodoModel.on("stateChanged", function(event, state){
            console.log(state);
            soundManager.play(state);
        });
    },

    initSounds = function(){
        soundManager.createSound({
            id: 'jump',
            url: 'res/sounds/dodoJump.mp3',
            autoLoad: true
        }).load();
        soundManager.createSound({
            id: 'slide',
            url: 'res/sounds/DodoSlide.mp3',
            autoLoad: true,
            autoPlay: false
        }).load();
        soundManager.createSound({
            id: 'die',
            url: 'res/sounds/DodoDie.mp3',
            autoLoad: true,
            autoPlay: false
        }).load();
        soundManager.createSound({
            id: 'shoot',
            url: 'res/sounds/PlaceTrap.mp3',
            autoLoad: true,
            autoPlay: false
        }).load();
        soundManager.createSound({
            id: 'pony',
            url: 'res/sounds/PonyThrow.mp3',
            autoLoad: true,
            autoPlay: false
        }).load();
        soundManager.createSound({
            id: 'dumdum',
            url: 'res/sounds/Manic Polka.mp3',
            loop: true,
            autoLoad: true,
            autoPlay: true
        }).load();
    }

    return {
        init : init
    }

}(jQuery, DodoRunner));


